﻿using Echart.BLL.Echarts;
using Echart.Model.Option;
using Echart.Model.Sample;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace Echart.DSL.Winform
{
    //设置Com对外可访问
    [System.Runtime.InteropServices.ComVisible(true)]
    public partial class Form1 : Form
    {
        public bool _isDocumentLoaded { get; set; }

        public Form1()
        {
            InitializeComponent();

            //初始化浏览器
            this.initWebBrowser();

            //加载 文件
            this.getAllHtmlFile();
        }

        /// <summary>
        /// 初始化浏览器
        /// </summary>
        private void initWebBrowser()
        {
            //防止 WebBrowser 控件打开拖放到其上的文件。
            webBrowser1.AllowWebBrowserDrop = false;

            //防止 WebBrowser 控件在用户右击它时显示其快捷菜单.
            webBrowser1.IsWebBrowserContextMenuEnabled = false;

            //以防止 WebBrowser 控件响应快捷键。
            webBrowser1.WebBrowserShortcutsEnabled = false;

            //以防止 WebBrowser 控件显示脚本代码问题的错误信息。    
            webBrowser1.ScriptErrorsSuppressed = true;

            //（这个属性比较重要，可以通过这个属性，把WINFROM中的变量，传递到JS中，供内嵌的网页使用；但设置到的类型必须是COM可见的，所以要设置     [System.Runtime.InteropServices.ComVisibleAttribute(true)]，因为我的值设置为this,所以这个特性要加载窗体类上）
            webBrowser1.ObjectForScripting = this; 
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            var manager = EchartManager.Current;
        }

        /// <summary>
        /// 获取html文件
        /// </summary>
        private void getAllHtmlFile()
        {
            //string[] name1 = Bar.barGraph;
            //string[] name2 = Line.lineGraph;
            string[] name3 = Pie.pieGraph;

            List<string> chartName = new List<string>();
            //foreach (var item in name1)
            //{
            //    chartName.Add(item);
            //}
            //foreach (var item in name2)
            //{
            //    chartName.Add(item);
            //}
            foreach (var item in name3)
            {
                chartName.Add(item);
            }
            foreach (var item in chartName)
            {
                comboBox1.Items.Add(item);
            }

            ////获取指定文件夹的所有文件  
            //string[] paths = Directory.GetFiles(str);
            //foreach (var item in paths)
            //{
            //    //获取文件后缀名  
            //    string extension = Path.GetExtension(item).ToLower();
            //    if (extension == ".html")
            //    {
            //        comboBox1.Items.Add(Path.GetFileName(item));
            //    }
            //}

            //if (comboBox1.Items.Count > 0)
            //{
            //    comboBox1.SelectedIndex = 0;
            //    this.webBrowser1.Url = new Uri(str + "\\" + comboBox1.Text.Trim());
            //}
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Refresh();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(comboBox1.SelectedItem == null)
            {
                return;
            }
            if (EchartManager.Current.HtmlManager.IsHtmlExist(comboBox1.Text.Trim()+".html"))
            {
                //this.webBrowser1.Url = new Uri(str + "\\" + comboBox1.Text.Trim());
            }
            else
            {
                EchartManager.Current.HtmlManager.CreateHtml(comboBox1.Text.Trim() + ".html");
            }
         
            DirectoryInfo info = new DirectoryInfo(Application.StartupPath);

            string str = info.Parent.Parent.Parent.FullName + "\\Echart.Foreground.Echarts\\echarts";
            webBrowser1.Navigate(str + "\\" + comboBox1.Text.Trim() + ".html");
            webBrowser1.ObjectForScripting = this;
          
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.webBrowser1.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            this.getAllHtmlFile();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            string option = EchartManager.Current.OptionManager.GetOption(comboBox1.Text.Trim());
            object[] objArray = new object[] { option };
            HtmlDocument htmlDocument = webBrowser1.Document;
            if (htmlDocument != null)
            {
                htmlDocument.InvokeScript("showChart", objArray);
                objArray[0] = Height - 40;
                htmlDocument.InvokeScript("setPosition", objArray);
                _isDocumentLoaded = true;
            }
        }

        private void webBrowser1_SizeChanged(object sender, EventArgs e)
        {
            if (_isDocumentLoaded)
            {
                object[] objArray = new object[] { Height - 40 };
                HtmlDocument htmlDocument = webBrowser1.Document;
                if (htmlDocument != null)
                {
                    htmlDocument.InvokeScript("setPosition", objArray);
                }
            }
        }      
    }
}
